package com.goldplusgold.tradedelay.app.home.live.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.goldplusgold.live.bean.VRecordBean;
import com.goldplusgold.imageloader.lib.IShow;
import com.goldplusgold.support.lib.base.adapter.BaseListAdapter;
import com.goldplusgold.support.lib.widget.pulltorefresh.PullToRefreshListView;
import com.goldplusgold.tradedelay.app.R;
import com.goldplusgold.tradedelay.app.home.live.LiveConstants;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/7/12.
 */

public class RecordsAdapter extends BaseListAdapter<List<VRecordBean>> {

    private List<VRecordBean> datas = new ArrayList<>();
    private Context context;
    int totalHeight = 0;
    ResizeViewInterface resizeViewInterface;
    public RecordsAdapter(Context context,ResizeViewInterface resizeViewInterface){
        this.context = context;
        this.resizeViewInterface = resizeViewInterface;
    }

    @Override
    public void upRereshView() {

    }

    @Override
    public void downRereshView() {

    }

    @Override
    public void setDatas(List<VRecordBean> list) {
        this.datas = list;
    }

    @Override
    public void setContext(Context context) {

    }

    @Override
    public void requestData() {

    }

    @Override
    public void setPullView(PullToRefreshListView listView) {

    }

    @Override
    public void notifyChange() {

    }

    @Override
    public int getCount() {
        if(datas != null){
            return datas.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if(datas != null){
            datas.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.video_item_layout, null);
            holder = new ViewHolder();
            holder.control = (ImageView) convertView.findViewById(R.id.video_item_layout_img_control);
            holder.title = (TextView) convertView.findViewById(R.id.video_item_layout_txt_title);
            holder.time = (TextView) convertView.findViewById(R.id.video_item_layout_txt_time);
            holder.back = (ImageView) convertView.findViewById(R.id.video_item_layout_img_back);
            holder.category = (TextView)convertView.findViewById(R.id.video_item_layout_txt_category);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }
        bindData(holder,position);
        final View view = convertView;
        view.post(new Runnable() {
            @Override
            public void run() {
                if(resizeViewInterface != null){
                    resizeViewInterface.resize(view.getHeight()*datas.size());
                }

            }
        });
        return convertView;
    }

    private void bindData(ViewHolder holder, final int position){
        holder.time.setText(datas.get(position).getLiveTime());
        IShow.getInstance().showRound(context,datas.get(position).getLiveIcon(),holder.back,false);
        holder.title.setText(datas.get(position).getLiveName());
        if(datas.get(position).getLiveType() == LiveConstants.LiveType.WORD_LIVE){
            holder.category.setText(R.string.live_wtext);
        } else{
            holder.category.setText(R.string.live_vtext);
        }
    }

    @Override
    public int getViewTypeCount() {
        return 1;
    }

    public static class ViewHolder {
        public TextView title;
        public ImageView back;
        public TextView time;
        public ImageView control;
        public TextView category;
    }

    public interface ResizeViewInterface{
        void resize(int height);
    }
}
